// https_get.js


const https = require('https');
const fs = require('fs');
const path = require('path');
var iconv = require('iconv-lite');

let getUrl = 'http://nodejs.cn/index.json';
getUrl = 'https://search.51job.com/list/040000,000000,0000,00,9,99,%25E5%2589%258D%25E7%25AB%25AF,2,1.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare=';

https.get(getUrl, (res) => {
    const { statusCode } = res;
    const contentType = res.headers['content-type'];

    let error;
    if (statusCode !== 200) {
        error = new Error(`请求失败\n状态码: ${statusCode}`);
    }

    if (error) {
        console.error(error.message);
        res.resume();
        return;
    }

    // res.setEncoding('gbk');
    // 解决乱码问题
    // 参考: https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding
    let rawData = [];
    res.on('data', (chunk) => {
        rawData.push(chunk);
    });
    res.on('end', ()=>{
        let decodedBody = iconv.decode(Buffer.concat( rawData ), 'gbk');
        // writeToFile(decodedBody);
        console.log(decodedBody);
    });
}).on('error', (e) => {
    console.error(`出现错误: ${e.message}`);
});

function writeToFile(rawData) {
    fs.writeFileSync(
        path.join(__dirname, '51jobdata.html'), 
        rawData, 
        {encoding:'utf8'});
}


